package com.itheima.reggie.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.reggie.entity.*;
import com.itheima.reggie.mapper.UserMapper;
import com.itheima.reggie.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceImpl implements UserService {
    @Autowired(required = false)
    private UserMapper userMapper;


    @Override
    public User login(String inputCode, String verifyCode, String phone) {
        User user = null;
        if (inputCode.equals(verifyCode)){
            user = userMapper.findByPhone(phone);
            if (user == null){
                user = new User();
                user.setPhone(phone);
                user.setStatus(1);
                userMapper.save(user);
            }
        }
        return user;
    }

    @Override
    public R<Page<Orders>> userPage(Integer page, Integer pageSize) {
        PageHelper.startPage(page,pageSize);



        List<Orders> ordersPage =  userMapper.userPage(page,pageSize);

        PageInfo<Orders> ordersPageInfo = new PageInfo<>(ordersPage);

        Page<Orders> orders = new Page<>(ordersPageInfo.getList(), ordersPageInfo.getTotal());
        return R.success(orders);

    }
}
